package k2_oop.p9_collection.demo09;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WorldCup {

    //存放所有冠军队伍的集合
    List<Team> teams = new ArrayList<>();

    {
        teams = Arrays.asList(
                new Team(1,1930,"乌拉圭"),
                new Team(2,1934,"意大利"),
                new Team(3,1938,"意大利"),
                new Team(4,1950,"乌拉圭"),
                new Team(5,1954,"德国"),
                new Team(6,1958,"巴西"),
                new Team(7,1962,"巴西"),
                new Team(8,1966,"英格兰"),
                new Team(9,1970,"巴西"),
                new Team(10,1974,"德国"),
                new Team(11,1978,"阿根廷"),
                new Team(12,1982,"意大利"),
                new Team(13,1986,"乌拉圭"),
                new Team(14,1990,"德国"),
                new Team(15,1994,"巴西"),
                new Team(16,1998,"法国"),
                new Team(17,2002,"巴西"),
                new Team(18,2006,"意大利")
        );
    }

    /**
     * 根据年份获取当前冠军队伍
     * 思想：
     *     利用 年份 创建一个Team的实例化对象
     *     判断这个对象是否在集合中存在
     * @param year
     */
    public void getTeamByYear(int year){

        int index = teams.indexOf(new Team(year));

        if(index>=0){
            System.out.println(teams.get(index).getChampionTeam());
        }else{
            System.out.println("没有举办世界杯");
        }
    }

    /**
     * 根据队伍名获取所有获得冠军的年份
     * @param team
     */
    public void getYearListByTeam(String team){

        ArrayList<Integer> years = new ArrayList<>();

        for (Team t : teams) {
            if (t.getChampionTeam().equals(team)) {
                years.add(t.getYear());
            }
        }

        if(years!=null && !years.isEmpty()){
            System.out.println(years);
        }else{
            System.out.println("没有获得过世界杯");
        }

    }

    public static void main(String[] args) {

        WorldCup worldCup = new WorldCup();
        worldCup.getTeamByYear(2006);
        worldCup.getYearListByTeam("中国");

    }


}
